angular.module('app').controller('productCtrl', ['$scope', '$rootScope', 'utils', '$stateParams', '$state', 'product', '$http', 'userInfo', function($scope, $rootScope, utils, $stateParams, $state, product, $http, userInfo) {

	//循环
	$scope.CountRepeat = function(num) {
		return new Array(num)
	}

	if($state.current.name == "productInfo") {
		$scope.productList = [] //项目信息
		$scope.PurchaseList = [] //已购用户
		$scope.$on('$viewContentLoaded', function(event) {
			setTimeout(function() {
				/*初始化slider*/
				var Slider = mui('#productTab').slider()
				/*设置滚动区域高度*/
				$('.mui-content').css('height', $(window).height() - 44 + 'px')
				$('.mui-slider-group').css('height', $(window).height() - $('.mui-slider-group').offset().top - 55 + 'px');
				$('.empty').css('height', $(window).height() - $('.mui-slider-group').offset().top - 70 + 'px')
				mui('.mui-scroll-wrapper').scroll({
					indicators: false,
				});
				/*手动触发滑动*/
				$(".product_Tab_Title").find("span").on("click", function() {
					Slider.gotoItem($(this).index())
					$(".product_Tab_Title").find("span").eq($(this).index()).addClass("mui-active").siblings().removeClass("mui-active")
				})
				/*监听事件*/
				$('#productTab').on("slide", function(e) {
					$(".product_Tab_Title").find("span").eq(e.detail.slideNumber).addClass("mui-active").siblings().removeClass("mui-active")
				})
			}, 200)
		});

		$scope.jisuan_show = false
		/*产品信息*/
		if(product.productInfo) {
			$scope.productInfo = {}
			angular.extend($scope.productInfo, product.productInfo)

		}
		/*获取项目信息*/
		$http.get(utils.productSys, {
			params: {
				header: utils.header("030030"),
				body: {
					id: $stateParams.productId,
					productTypePid:$scope.productInfo.product_type_pid
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				$scope.productList = res.body.debtList
			}
		})

		/*获取已购用户*/
		$http.get(utils.OrderSys, {
			params: {
				header: utils.header("010050"),
				body: {
					begin_index: 0,
					count: 100,
					pro_id: $stateParams.productId
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				$scope.PurchaseList = res.body.list
			}
		})

		$scope.buy = function(item) {
			userInfo.buy(item)
		}

		mui(document).on("tap", ".mui-backdrop", function() {
			$scope.jisuan_show = false
			$scope.$apply()
		})

		$scope.Profit = 0 //收益


		$scope.gourl=function(item){
			 if(item.debt_status_id_type==2){
				$state.go("product_details_new",{
					debtId:item.debt_code
				 })
			 }else{
				 $state.go("productDetails",{
					debtId:item.debt_code
				 })
			 }
		}

		$scope.jisuan = function() {

			var rate = $scope.productInfo.benchmark_return_rate * 1 + $scope.productInfo.increase_interest_return_rate * 1
			/*活期*/
			if($scope.productInfo.product_type_pid == 4) {
				$scope.Profit = $scope.pro_money * $scope.pro_day * rate / 365
			} else { //定期
				$scope.Profit = $scope.pro_money * $scope.productInfo.term * rate / 365
			}

		}

	}

	if($state.current.name == "bondList") {
		if($scope.isapp) {
			wv.set_title("投资债权")

		}
		$scope.bondInfo = []
		switch($stateParams.type) {
			case "3":
				$http.post(utils.balanceSys, {}, {
					params: {
						header: utils.header("000023"),
						body: {
							assets_id: $stateParams.assetsId,
							page_index: 1,
							page_size: 100
						}
					}
				}).then(function(res) {
					if(res.header.re_code == "0000") {
						$scope.bondInfo = res.body
					}
				})
				break;
			case "4":
				$http.post(utils.balanceSys, {}, {
					params: {
						header: utils.header("000018"),
						body: {
							assets_id: $stateParams.assetsId,
							page_index: 1,
							page_size: 100
						}
					}
				}).then(function(res) {
					if(res.header.re_code == "0000") {
						$scope.bondInfo = res.body
					}
				})
				break;
			case "1":
				$http.post(utils.balanceSys, {}, {
					params: {
						header: utils.header("000029"),
						body: {
							assets_id: $stateParams.assetsId,
							page_index: 1,
							page_size: 100
						}
					}
				}).then(function(res) {
					if(res.header.re_code == "0000") {
						$scope.bondInfo = res.body
					}
				})
				break;
		}

	}

	if($state.current.name == "productDetails") {

		if($scope.isapp) {
			wv.set_title("项目详情")

		}
		$http.get(utils.productSys, {
			params: {
				header: utils.header("030113"),
				body: {
					debtCode: $stateParams.debtId
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				$scope.productDetails = res.body
				picLazyLoad({
					className: "lazyload"
				});
			}
		})
	}

	/*风险分散*/
	if($state.current.name == "fxfs") {
		$http.post(utils.balanceSys, {}, {
			params: {
				header: utils.header("000036"),
				body: {
					assets_id: $stateParams.assetsId
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				$scope.fxList = res.body
			}

		})
	}

	/*产品风险分散*/
	if($state.current.name == "CpFxfs") {
		$http.get(utils.productSys, {
			params: {
				header: utils.header("030502"),
				body: {
					productId: $stateParams.assetsId
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				$scope.fxList = res.body.debts
			}

		})
	}

	/*项目详情新*/
	if($state.current.name == "product_details_new") {
		/*初始化slider*/
		var Slider = mui('#productTab').slider()
		$scope.$on('$viewContentLoaded', function(event) {
			setTimeout(function() {
				/*设置滚动区域高度*/
				$('.mui-content').css('height', $(window).height() - 44 + 'px')
				if($scope.isapp) {
					$('.mui-slider-group').css('height', $(window).height() - $('.mui-slider-group').offset().top + 'px');
					$('.mui-scroll').css('min-height', $(window).height() - $('.mui-slider-group').offset().top + 'px')
				} else {
					$('.mui-slider-group').css('height', $(window).height() - $('.mui-slider-group').offset().top + 'px');
					$('.mui-scroll').css('min-height', $(window).height() - $('.mui-slider-group').offset().top + 'px')
				}

				mui('.mui-scroll-wrapper').scroll({
					scrollY: true, //是否竖向滚动
					scrollX: false, //是否横向滚动
					startX: 0, //初始化时滚动至x
					startY: 0, //初始化时滚动至y
					indicators: true, //是否显示滚动条
					deceleration: 0.0006, //阻尼系数,系数越小滑动越灵敏
					bounce: true //是否启用回弹
				});
			}, 200)

		})

		/*手动触发滑动*/
		$(".product_Tab_Title").find("span").on("click", function() {
			Slider.gotoItem($(this).index())
			$(".product_Tab_Title").find("span").eq($(this).index()).addClass("mui-active").siblings().removeClass("mui-active")
		})
		/*监听事件*/
		$('#productTab').on("slide", function(e) {
			$(".product_Tab_Title").find("span").eq(e.detail.slideNumber).addClass("mui-active").siblings().removeClass("mui-active")
		})

		if($scope.isapp) {
			wv.set_title("项目详情")

		}
		/*获取项目基本信息*/
		$http.get(utils.productSys, {
			params: {
				header: utils.header("030113"),
				body: {
					debtCode: $stateParams.debtId
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				$scope.productDetails = res.body
				picLazyLoad({
					className: "lazyload"
				});
			}
		})

		/*获取贷后追踪/还款计划*/
		$http.get(utils.productSys, {
			params: {
				header: utils.header("030506"),
				body: {
					debtCode: $stateParams.debtId
				}
			}
		}).then(function(res) {

			if(res.header.re_code == "0000") {
				$scope.other = res.body
			}
		})

	}
	
	/*安全等级和投资建议*/
	
	if($state.current.name == "risk_proposal"){
		
		
		
		$scope.$on('$viewContentLoaded', function(event) {
			setTimeout(function() {
				$http.get(utils.productSys,{
					params:{
						header:utils.header("030512"),
						body:{
							
						}
					}
				}).then(function(res){
					if(res.header.re_code=="0000"){
						$scope.assetlimitList=res.body.assetLimit
					}
				})
				
				$http.get(utils.productSys,{
					params:{
						header:utils.header("030509"),
						body:{
							
						}
					}
				}).then(function(res){
					if(res.header.re_code=="0000"){
						$scope.roskInfo=res.body.risk_user_level
					}
				})

               


			}, 200)

		})
		
		
		
	}
	
	
	

}])